package com.example.hotbar;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;


import android.widget.EditText;

import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;



public class AddBarsActivity extends Activity implements OnClickListener {
	
	Button sqlSubmit, sqlView;
	EditText addBarsNameEditText,addBarsCityEditText;
	
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_bars);
		
		sqlView = (Button) findViewById(R.id.showDB);
		sqlSubmit = (Button) findViewById(R.id.submitBar);
		addBarsNameEditText = (EditText) findViewById(R.id.addBarsNameEditText);
		
		addBarsCityEditText = (EditText) findViewById(R.id.addBarsCityEditText);
		
		sqlView.setOnClickListener(this);
		sqlSubmit.setOnClickListener(this);
		
		
		
		
		//SQLiteDatabase.openOrCreateDatabase("HotSpotDB",null);
		//db=openOrCreateDatabase("HotSpotDB1", SQLiteDatabase.CREATE_IF_NECESSARY , null);
		
		//db.execSQL("CREATE TABLE IF NOT EXISTS Bar(barName VARCHAR, barLocation VARCHAR);");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add_bars, menu);
		return true;
	}
	
@Override
public void onClick(View arg0) {
	// TODO Auto-generated method stub
	switch (arg0.getId()){
	
	case R.id.submitBar:
		boolean didItWork = true;
		try{
			String name = addBarsNameEditText.getText().toString();
			String location = addBarsCityEditText.getText().toString();
			HotBarDB entry = new HotBarDB(AddBarsActivity.this);
			entry.open();
		    //entry.getData();
		   boolean dataChecked = entry.checkExistingData(name,location);
		    
		   if (dataChecked == true)
		   {
			   entry.createEntry(name,location);
			   entry.close();
			   Dialog d = new Dialog(this);
				d.setTitle("Completed!");
				TextView tv = new TextView(this);
				tv.setText("Success");
				d.setContentView(tv);
				d.show();
		   }
		   
		   	else
		   	{
		   		Dialog d = new Dialog(this);
				d.setTitle("Couldnt Update!");
				TextView tv = new TextView(this);
				tv.setText("Already found!");
				d.setContentView(tv);
				d.show();
		   	}
				 								
		}catch (Exception e){
				didItWork = false;
				String error = e.toString();
				Dialog d = new Dialog(this);
				d.setTitle("Dang IT!");
				TextView tv = new TextView(this);
				tv.setText(error);
				d.setContentView(tv);
				d.show();
		}
			break;
		
	case R.id.showDB:
		//Intent i = new Intent(AddBarsActivity.this, DataView"com.example.hotbar.DataView");
		
		Intent i =new Intent(AddBarsActivity.this,DataView.class);
		startActivity(i);
	    
		
		break;
		}
	}
}

//@Override
//public void onClick(View v) {
	// TODO Auto-generated method stub
	//v.getId();
	//Intent mainActivity = new Intent(AddBarsActivity.this,MainActivity.class);       	  	      
    //startActivity(mainActivity);
//}



	
	






